package com.zhb.untils;
import com.jcraft.jsch.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Properties;

@Component
public class UpOrDown {

    @Value("${linux.hostname}")
    private  String remoteHost;
    @Value("${linux.password}")
    private  String password;
    @Value("${linux.port}")
    private  int port;
    @Value("${linux.username}")
    private  String username;
    public  void download(String localFilePath,String remoteFilePath) {

        // String remoteFilePath = "/root/DirTongsuo/"+name;

        JSch jsch = new JSch();
        Session session = null;
        try {
            session = jsch.getSession(username, remoteHost);
            session.setPassword(password);

            // Avoid asking for key confirmation
            Properties prop = new Properties();
            prop.put("StrictHostKeyChecking", "no");
            session.setConfig(prop);

            session.connect();

            // Create SFTP channel
            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            // Download file
            InputStream inputStream = channelSftp.get(remoteFilePath);
            OutputStream outputStream = new FileOutputStream(new File(localFilePath));
            byte[] buffer = new byte[1024];
            int bytesRead = -1;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }

            System.out.println("File downloaded successfully!");

            channelSftp.disconnect();
            session.disconnect();
        } catch (JSchException | SftpException | IOException e) {
            e.printStackTrace();
        }

    }
    public String Upload (String localFilePath, String name){
        // String user = "root";
        // String password = "****";
        // String host = "47.94.4.57";
        // int port = 22;
        String remoteFilePath = "/root/DirTongsuo/"+name;
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, remoteHost, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            channel.put(localFilePath, remoteFilePath);
            channel.disconnect();
            session.disconnect();
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        }
        return remoteFilePath;
    }
        public static void download2Http(String filename, HttpServletResponse res) throws IOException {
            // 发送给客户端的数据
            OutputStream outputStream = res.getOutputStream();
            byte[] buff = new byte[1024];
            BufferedInputStream bis = null;
            // 读取filename
            bis = new BufferedInputStream(new FileInputStream(new File(filename)));
            int i = bis.read(buff);
            while (i != -1) {
                outputStream.write(buff, 0, buff.length);
                outputStream.flush();
                i = bis.read(buff);
            }
        }



}

